//
//  ReceiveCellEnterViewController.m
//  YKMediator_ReceiveModule
//
//  Created by linghit on 2021/11/2.
//

#import "ReceiveCellEnterViewController.h"
#import "ReceiveViewModel.h"
#import <YKAlert/YKAlertCenter.h>
#import <Masonry/Masonry.h>
#import <YK_ExectionModel/YKExection.h>

@interface ReceiveCellEnterViewController ()
///
@property (nonatomic, strong, readwrite) ReceiveViewModel *viewModel;
///
@property (nonatomic, strong, readwrite) UITextField *priceTextField;
///
@property (nonatomic, strong, readwrite) UITextField *titleTextField;
///
@property (nonatomic, strong, readwrite) UIButton *comfirmButton;
@end

@implementation ReceiveCellEnterViewController

- (instancetype)initWithViewModel:(ReceiveViewModel *)viewModel
{
    self = [super init];
    if (self) {
        self.viewModel = viewModel;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didSetupUI
{
    [super didSetupUI];
    self.priceTextField = [[UITextField alloc] init];
    self.priceTextField.placeholder = @"请输入收款价格";
    self.priceTextField.font = [UIFont systemFontOfSize:20];
    self.priceTextField.keyboardType = UIKeyboardTypeDecimalPad;
    self.priceTextField.layer.borderWidth = 1;
    self.priceTextField.layer.borderColor = UIColor.blackColor.CGColor;
    
    [self.view addSubview:self.priceTextField];
    [self.priceTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.centerY.mas_equalTo(self.view.mas_centerY).offset(-lengthScale(200));
        make.size.mas_equalTo(CGSizeMake(lengthScale(300), lengthScale(30)));
    }];
    
    self.titleTextField = [[UITextField alloc] init];
    self.titleTextField.placeholder = @"请输入与备注（可以不输入）";
    self.titleTextField.font = [UIFont systemFontOfSize:20];
    self.titleTextField.layer.borderWidth = 1;
    self.titleTextField.layer.borderColor = UIColor.blackColor.CGColor;
    [self.view addSubview:self.titleTextField];
    [self.titleTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(self.priceTextField.mas_bottom).offset(lengthScale(20));
        make.size.mas_equalTo(CGSizeMake(lengthScale(300), lengthScale(30)));
    }];
    
    self.comfirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.comfirmButton setTitle:@"提 交" forState:UIControlStateNormal];
    [self.comfirmButton setTitleColor:YK_WHILE forState:UIControlStateNormal];
    [self.comfirmButton setBackgroundImage:YKHEXCOLOR(@"#FF0000").image forState:UIControlStateNormal];
    self.comfirmButton.titleLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightBold];
    self.comfirmButton.layer.cornerRadius = lengthScale(20);
    self.comfirmButton.clipsToBounds = YES;
    [self.view addSubview:self.comfirmButton];
    [self.comfirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(self.titleTextField.mas_bottom).offset(lengthScale(20));
        make.size.mas_equalTo(CGSizeMake(lengthScale(200), lengthScale(40)));
    }];
    
    
}

- (void)didBindData
{
    [super didBindData];
    @weakify(self);
    [[self.viewModel.uploadCellCommand.executing skip:1] subscribeNext:^(NSNumber * _Nullable x) {
        if ([x boolValue]) {
            [YKAlertCenter showLoading:@"正在上传"];
        }else{
            [YKAlertCenter dissLoading];
        }
    }];
    
    [self.viewModel.uploadCellCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [YKAlertCenter showMessage:@"上传成功"];
        self.priceTextField.text = @"";
        self.titleTextField.text = @"";
        [self.priceTextField endEditing:YES];
        [self.titleTextField endEditing:YES];
    }];
    
    [self.comfirmButton yk_handleControlEvent:UIControlEventTouchUpInside withBlock:^(UIButton *button) {
        @strongify(self);
        [self.viewModel.uploadCellCommand execute:@{@"name":self.titleTextField.text,@"price":self.priceTextField.text}];
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
